﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Resources;
using Windows.ApplicationModel.Resources.Core;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;

namespace ShopPowerTemplate.Common
{
    public class ZipHelper
    {
        public static async Task<BitmapImage> ExtractImage(byte[] image)
        {
            BitmapImage result = new BitmapImage();

            var zipArchive = new ZipArchive(new MemoryStream(image));
            var entryStream = zipArchive.Entries.FirstOrDefault();

            if (entryStream != null)
            {
                byte[] buffer = new byte[entryStream.Length];
                entryStream.Open().Read(buffer, 0, buffer.Length);

                InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
                randomAccessStream = new InMemoryRandomAccessStream();
                DataWriter writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0));
                writer.WriteBytes(buffer);
                await writer.StoreAsync();

                await result.SetSourceAsync(randomAccessStream);
            }
            
            return result;
        }

        internal static async Task<IRandomAccessStream> GetImageAsStream(byte[] image)
        {
            InMemoryRandomAccessStream randomAccessStream = null;
            var zipArchive = new ZipArchive(new MemoryStream(image));
            var entryStream = zipArchive.Entries.FirstOrDefault();

            if (entryStream != null)
            {
                byte[] buffer = new byte[entryStream.Length];
                entryStream.Open().Read(buffer, 0, buffer.Length);

                randomAccessStream = new InMemoryRandomAccessStream();
                DataWriter writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0));
                writer.WriteBytes(buffer);
                await writer.StoreAsync();
            }
            return randomAccessStream;
        }

        internal async static Task<string> ExtractToLocalStore(byte[] image, string filename)
        {
            var zipArchive = new ZipArchive(new MemoryStream(image));
            var entryStream = zipArchive.Entries.FirstOrDefault();
            string result = null;

            if (entryStream != null)
            {
                string extension = Path.GetExtension(entryStream.Name);
                result = Path.ChangeExtension(filename, extension);
                byte[] buffer = new byte[entryStream.Length];
                entryStream.Open().Read(buffer, 0, buffer.Length);

                StorageFolder folder = ApplicationData.Current.LocalFolder;
                StorageFile file = await folder.CreateFileAsync(result, CreationCollisionOption.ReplaceExisting);
                using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    using (IOutputStream outputStream = fileStream.GetOutputStreamAt(0))
                    {
                        using (DataWriter dataWriter = new DataWriter(outputStream))
                        {
                            dataWriter.WriteBytes(buffer);
                            await dataWriter.StoreAsync();
                            dataWriter.DetachStream();
                        }

                        await outputStream.FlushAsync();
                    }
                }

                result = "ms-appdata:///local/" + Path.ChangeExtension(filename, extension);
            }

            return result;
        }
    }
}
